package com.example.administrator.szb.fragments.fragment_forTab.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.bean.IndexBean;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;

/**
 * Created by stone on 2018/1/19.
 */

public class JGadapter extends BaseAdapter {
    private Context context;
    private List list;
    LayoutInflater inflater;
    public JGadapter(Context context, List list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public  IndexBean.DataBean.HotCounBean getItem(int position) {
        return ( IndexBean.DataBean.HotCounBean)list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.item_shocang_jg,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(context).load(getItem(position).getZhenshi_img()).error(R.mipmap.default_header_s).into(holder.head_img);
        holder.name.setText(getItem(position).getName());
        holder.address.setText(getItem(position).getCompany());
        String[] l = (getItem(position).getCity()).split("-");
        holder.tv_addresss.setText(l[1]);
        showYW(holder,getItem(position).getBusiness_id());
        showStar(holder,getItem(position).getEvaluate());
        return convertView;
    }
    private void showStar(ViewHolder holder, int star){
        switch (star){
            case 10:
                holder.iv_1.setImageResource(R.drawable.start);
                holder.iv_2.setImageResource(R.drawable.start);
                holder.iv_3.setImageResource(R.drawable.start);
                holder.iv_4.setImageResource(R.drawable.start);
                holder.iv_5.setImageResource(R.drawable.start);
                break;
            case 9:

                holder.iv_1.setImageResource(R.drawable.start);
                holder.iv_2.setImageResource(R.drawable.start);
                holder.iv_3.setImageResource(R.drawable.start);
                holder.iv_4.setImageResource(R.drawable.start);
                holder.iv_5.setImageResource(R.drawable.star_half);
                break;
            case 8:
                holder.iv_1.setImageResource(R.drawable.start);
                holder.iv_2.setImageResource(R.drawable.start);
                holder.iv_3.setImageResource(R.drawable.start);
                holder.iv_4.setImageResource(R.drawable.start);
                holder.iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 7:
                holder.iv_1.setImageResource(R.drawable.start);
                holder.iv_2.setImageResource(R.drawable.start);
                holder.iv_3.setImageResource(R.drawable.start);
                holder.iv_4.setImageResource(R.drawable.star_half);
                holder.iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 6:

                holder.iv_1.setImageResource(R.drawable.start);
                holder.iv_2.setImageResource(R.drawable.start);
                holder.iv_3.setImageResource(R.drawable.start);
                holder.iv_4.setImageResource(R.drawable.star_hui);
                holder.iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 5:
                holder.iv_1.setImageResource(R.drawable.start);
                holder.iv_2.setImageResource(R.drawable.start);
                holder.iv_3.setImageResource(R.drawable.star_half);
                holder.iv_4.setImageResource(R.drawable.star_hui);
                holder.iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 4:
                holder.iv_1.setImageResource(R.drawable.start);
                holder.iv_2.setImageResource(R.drawable.start);
                holder.iv_3.setImageResource(R.drawable.star_hui);
                holder.iv_4.setImageResource(R.drawable.star_hui);
                holder.iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 3:
                holder.iv_1.setImageResource(R.drawable.start);
                holder.iv_2.setImageResource(R.drawable.star_half);
                holder.iv_3.setImageResource(R.drawable.star_hui);
                holder.iv_4.setImageResource(R.drawable.star_hui);
                holder.iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 2:
                holder.iv_1.setImageResource(R.drawable.start);
                holder.iv_2.setImageResource(R.drawable.star_hui);
                holder.iv_3.setImageResource(R.drawable.star_hui);
                holder.iv_4.setImageResource(R.drawable.star_hui);
                holder.iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 1:

                holder.iv_1.setImageResource(R.drawable.star_half);
                holder.iv_2.setImageResource(R.drawable.star_hui);
                holder.iv_3.setImageResource(R.drawable.star_hui);
                holder.iv_4.setImageResource(R.drawable.star_hui);
                holder.iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 0:
                holder.iv_1.setImageResource(R.drawable.star_hui);
                holder.iv_2.setImageResource(R.drawable.star_hui);
                holder.iv_3.setImageResource(R.drawable.star_hui);
                holder.iv_4.setImageResource(R.drawable.star_hui);
                holder.iv_5.setImageResource(R.drawable.star_hui);
                break;
        }

    }

    /**
     * 显示业务
     * @param holder
     * @param list
     */
    private void showYW(ViewHolder holder, List< IndexBean.DataBean.HotCounBean.BusinessIdBean> list){
        holder.tv_1.setVisibility(View.GONE);
        holder.tv_2.setVisibility(View.GONE);
        holder.tv_3.setVisibility(View.GONE);
        holder.tv_4.setVisibility(View.GONE);
        for (int i=0;i<list.size();i++){
            switch (i){
                case 0:
                    holder.tv_1.setText(list.get(i).getTitle());
                    holder.tv_1.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    holder.tv_2.setText(list.get(i).getTitle());
                    holder.tv_2.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    holder.tv_3.setText(list.get(i).getTitle());
                    holder.tv_3.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    holder.tv_4.setText(list.get(i).getTitle());
                    holder.tv_4.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }
    class ViewHolder{

        View my_check_box;
        LinearLayout checkBox_ll;
        RoundedImageView head_img;
        ImageView iv_1,iv_2,iv_3,iv_4,iv_5;
        TextView name,address,tv_1,tv_2,tv_3,tv_4,tv_addresss;
        public ViewHolder(View view){
            tv_addresss = view.findViewById(R.id.tv_addresss);
            my_check_box = view.findViewById(R.id.my_check_box);
            checkBox_ll = view.findViewById(R.id.checkBox_ll);
            head_img = view.findViewById(R.id.head_img);
            iv_1 = view.findViewById(R.id.iv_1);
            iv_2 = view.findViewById(R.id.iv_2);
            iv_3 = view.findViewById(R.id.iv_3);
            iv_4 = view.findViewById(R.id.iv_4);
            iv_5 = view.findViewById(R.id.iv_5);
            name = view.findViewById(R.id.name);
            address = view.findViewById(R.id.address);
            tv_1 = view.findViewById(R.id.tv_1);
            tv_2 = view.findViewById(R.id.tv_2);
            tv_3 = view.findViewById(R.id.tv_3);
            tv_4 = view.findViewById(R.id.tv_4);
        }
    }
}
